<?php
/**
 * Created by PhpStorm.
 * User: wuchunyin
 * Date: 2018/11/15
 * Time: 16:13
 */

namespace app\index\ui;

class PrintTable {

    private static $newLine='1';
    private static  $formStr = [];

    static public function printTable($config,$data){

        $configLength = count($config);
        for($i=0;$i<$configLength;$i++) {
            $con = $config[$i];
            $con['VALUE'] = isset($data[$con['FIELD']])?$data[$con['FIELD']]:'';
            if($con['TYPE'] == 'text' || $con['TYPE'] == 'selectYear' || $con['TYPE'] == 'date'){
                self::$formStr[$i] = self::text($con,$i,$configLength);
            }
            else if($con['TYPE'] == 'select'){
                self::$formStr[$i]= self::select($con,$i,$configLength);
            }
        }
        return implode('',self::$formStr);
    }

    static public function text($con,$i,$configLength){
        $title = isset($con['TITLE'])?$con['TITLE']:'';
        $value = isset($con['VALUE'])?$con['VALUE']:'';
        $isOneLine = isset($con['IS_ONE_LINE'])?$con['IS_ONE_LINE']:'0';

        $html = '';
        if ($isOneLine === '0' AND self::$newLine==='1') {
            if ($i == ($configLength-1)) {
                $html = <<<EOF
                    <tr>
                        <th>$title</th>
                        <td colspan="3">$value</td>      
                    </tr> 
EOF;

            } else {
                $html = <<<EOF
                <tr>
                    <th>$title</th>
                    <td colspan="1">$value</td>
EOF;
            }

            self::$newLine = '0';
        } elseif ($isOneLine === '0' AND self::$newLine==='0') {
            $html = <<<EOF
            
                <th>$title</th>
                <td colspan="1">$value</td>       
            </tr>
EOF;
            self::$newLine = '1';
        } elseif ($isOneLine === '1' AND self::$newLine==='1') {
            $html = <<<EOF
            <tr>
                <th>$title</th>
                <td colspan="3">$value</td>       
            </tr>
EOF;
            self::$newLine = '1';
        } elseif ($isOneLine === '1' AND self::$newLine==='0') {
            self::$formStr[$i-1] = str_replace('colspan="1"','colspan="3"',self::$formStr[$i-1])."</tr>";
            $html = <<<EOF
            <tr>
                <th>$title</th>
                <td colspan="3">$value</td>       
            </tr>
EOF;
            self::$newLine = '1';
        }

        return $html;
    }

    static public function select($con,$i,$configLength){

        $title = isset($con['TITLE'])?$con['TITLE']:'';
        $name = isset($con['NAME'])?$con['NAME']:'';
        $value = isset($con['VALUE'])?$con['VALUE']:'';
        $s_url = isset($con['S_URL'])?$con['S_URL']:'';
        $url = url($s_url);
        $s_key = isset($con['S_KEY'])?$con['S_KEY']:'ID';
        $s_field = isset($con['S_FIELD'])?$con['S_FIELD']:'NAME';
        $isOneLine = isset($con['IS_ONE_LINE'])?$con['IS_ONE_LINE']:'0';

        $html = '';
        if ($isOneLine === '0' AND self::$newLine==='1') {
            if ($i == ($configLength-1)) {
                $html = <<<EOF
            <tr>
                <th>$title</th>
                <td colspan="3" name="$name"></td>  
            </tr>  

EOF;
    } else {
                $html = <<<EOF
                <tr>
                    <th>$title</th>
                    <td colspan="1" name="$name"></td>       

EOF;
            }

            self::$newLine = '0';
        } elseif ($isOneLine === '0' AND self::$newLine==='0') {
            $html = <<<EOF
            
                <th>$title</th>
                <td colspan="1" name="$name"></td>       
            </tr>

EOF;
            self::$newLine = '1';
        } elseif ($isOneLine === '1' AND self::$newLine==='1') {
            $html = <<<EOF
            <tr>
                <th>$title</th>
                <td colspan="3" name="$name"></td>       
            </tr>
EOF;
            self::$newLine = '1';
        } elseif ($isOneLine === '1' AND self::$newLine==='0') {
            self::$formStr[$i-1] = str_replace('colspan="1"','colspan="3"',self::$formStr[$i-1])."</tr>";
            $html = <<<EOF
            <tr>
                <th>$title</th>
                <td colspan="3" name="$name"></td>       
            </tr>
EOF;
            self::$newLine = '1';
        }

        $script = <<<EOF
            
<script>
    $(function() {
        $.ajax({
            url:"$url",
            type:'post',
            success:function(data) {
                console.log(data);
                for(var i = 0 ; i < data.length ; i++){
                    if("$value" == data[i]["$s_key"]){
                         var text = data[i]["$s_field"];
                         $("[name='$name']").html(text);
                    }
                }
            },
            error:function(res) {
                layer.alert("获取[$title]数据失败",{icon:2});
            }
        });
    });
</script>
EOF;
        return $html.$script;
    }

}